angular.module('purchase-detail', ['row-show'])

.controller 'PurchaseDetailCtrl', ($scope, $state, $http, $ionicHistory,$stateParams,orderPurchase,$ionicPopup,userData,$cordovaPrinter,showToast,$cordovaInAppBrowser,$rootScope) ->
    $scope.order = orderPurchase.order
    console.log $scope.order
    if $scope.order.orderStatus == '待发货' or $scope.order.orderStatus == '已完成'
        $scope.daifahuo = true
    else if $scope.order.orderStatus == '待收货'
        $scope.daishouhuo = true
    $scope.orderItems = []
    $http.get('/tradeapi/',params:{action:'mytradeinfo',userid:userData.getToken(),id:$scope.order.id})
    .success (resp) ->
        if resp?.status == '0'
            $scope.orderDetail=resp
            console.log $scope.orderDetail
            $scope.orderItems = []
            _.each resp.productlist,(item)->
                $scope.orderItems.push {
                    img:item.productimg
                    txt1:item.producttitle
                    txt2:item.productprice
                    txt3:item.productcount
                }
        else
            showToast resp.error
    .error ->
        showToast '网络出现异常'
    getWuliu = ->
        console.log $scope.orderDetail.sendcode
        if $scope.orderDetail.sendcode != ''
            $http.get("http://api.kuaidi100.com/api?&id=cfe1e9114f773ce2",params:{com:$scope.orderDetail.sendcode,nu:$scope.orderDetail.sendno,show:0,order:'desc'})
            .success (resp) ->
                if resp?.status == '1'
                    $scope.wuliuData = resp.data
                    console.log $scope.wuliuData
                    $scope.state = resp.state
                    console.log $scope.state
                else
                    if resp.status == '0'
                        showToast resp.message
                    else if resp.status == '2'
                        showToast resp.message
            .error ->
                showToast '网络处理异常'
        else
            return false

    $scope.options = {
        location: 'no'
        clearcache: 'no'
        toolbar: 'yes'
    }
    _.extend $scope,
        goPrinter: ->
            $http.get("http://www.upd365.com/apiforapp/printtrade/sendemail.asp?userid=#{userData.getToken()}&tradeid=#{$scope.order.id}")
            .success (resp) ->
                if resp?.status == '0'
                    showToast resp.msg
                else
                    showToast resp.msg
            .error ->
                showToast '您的网络又调皮了'
        goBack: ->
            $ionicHistory.goBack()

        remove:(orderId) ->
            $http.get('/tradeapi/',params:{action:'delshoptrade',userid:userData.getToken(),id:orderId})
            .success (resp) ->
                if resp?.status == '0'
                    showToast resp.msg
                    $ionicHistory.goBack()
                else
                    showToast resp.msg
            .error ->
                showToast '您的网络又调皮了'
        goPay:(TradeNo) ->
            if $scope.order.paytype == '0'
                $cordovaInAppBrowser.open("http://www.upd365.com/onlinepay/mobilepayforshop/?userid=#{userData.getToken()}&tradeno=#{TradeNo}", '_blank', $scope.options)
                .then (event) ->
                    logger.error 'event',JSON.stringify(event)
                .catch (event)->
                    console.log
            else
                return false
        goConfirm:(orderId) ->
            $http.post("/tradeapi/",{action:'shopreceipt',userid:userData.getToken(),id:orderId})
            .success (resp) ->
                if resp.status == '0'
                    showToast resp.msg
                    $ionicHistory.goBack()
                else
                    showToast resp.msg
            .error ->
                showToast '您的网络又调皮了'
    $scope.$on '$ionicView.enter',->
        getWuliu()

    $rootScope.$on '$cordovaInAppBrowser:loadstop', (e, event) ->
        logger.error 'e:',_.keys(e)
        logger.error 'event:',_.keys(event)





